#!/bin/sh

# DataXone Password Updater
# 1, OS User Password
# 2, DB User Password
#
# @Author: liaobaikai@dsgdata.com
# @Date:     2024-10-10
# @Update:   2024-10-30
# @Update:   2024-11-22
# @Version: 1.0.5
#
# Changelog:
# 1.0.3: Update Os password on DB.
# 1.0.5: Add Encryption and Decryption check.
# ---------------------------------------------
UPDATE=2024-11-22
VERSION=1.0.5
DEBUG=0
SCRIPT_NAME=$0
USERNAME_0=dataxone
DB_PORT_0=3306
DATAXONE_BASE_0=/data/dataxone
DATAXONE_WEB_APPS_0=$DATAXONE_BASE_0/www/webapps
DATAXONE_WEB_APPS_1=$DATAXONE_WEB_APPS_0
PWDCRYPT_DIR_0=$PWD/pwdcrypt
MANAGER_PWDCRYPT_DIR_0=$PWD/managerEncrypt

CONFIG_MASTER_SCRIPT=./config_master.sh
YML_MASTER_SCRIPT=./yml_master.sh
TMP_FILE=.tmp.properties

MYSQL_CLIENT=/usr/bin/mysql




Usage(){
    echo "
Usage: $SCRIPT_NAME COMMAND OPTIONS

DataXone Password Updater Ver $VERSION, Updated on $UPDATE

Commands:
  db      Update For Database User Password
  os      Update For OS User Password
  reset   Reset Dataxone Platform Login User Password (Not suited for ICBC version)

Options:
--dataxone-base=#          DataXone install base path. Default: /data/dataxone
--webapps=#                Tomcat webapps. Default: /data/dataxone/www/webapps
--dataxone-webapps=#       Dataxone-tomcat webapps. Default: /data/dataxone/www/webapps
--pwdcrypt-dir=#           Common password encrypt tools dir. Default: \$PWD/pwdcrypt
--manager-pwdcrypt-dir=#   Dataxone manager password encrypt tools dir. Default: \$PWD/managerEncrypt
--user=#                   Username, Default: dataxone
--password=#               Password for user
--db-password=#            Password for Database user
--db-host=#                Database host for check
--db-port=#                Database port for check, Default: 3306
--skip-precheck            Skip Database Precheck
--debug                    Turn on debug mode
-h, --help                 Print help
-v, --version              Print Version

Examples:
1) $SCRIPT_NAME db --user=dataxone --db-host=xxxxxxx --db-port=3306 --db-password=<new_password>
2) $SCRIPT_NAME os --user=dataxone --password=<new_password> --db-host=xxxxxxx --db-port=3306 --db-password=<db_password>

"
exit 0
}

get_config_file(){
    case $1 in
    permission) p="$DATAXONE_WEB_APPS_0/$1/WEB-INF/classes/application.properties" ;;
    autoMaticEngineBoot-1.0.0) p="$DATAXONE_WEB_APPS_1/$1/WEB-INF/classes/application.properties" ;;
    DataXone_Query_Engine) p="$DATAXONE_BASE_0/$1/query_db_engine/config/module/properties/jdbc.properties" ;;
    Manager_Node_Engine) p="$DATAXONE_BASE_0/$1/config/module/dataBase/properties/jdbc.properties" ;;
    icbc_automatic_flow_dsg_project-1.0.0) p="$DATAXONE_WEB_APPS_0/$1/WEB-INF/classes/application.yml" ;;
    sbt_xcmp) p="$DATAXONE_WEB_APPS_0/$1/WEB-INF/classes/application.properties" ;;
    esac
    echo $p
}


argument_count=$#
command=$1
shift
argument_count=`expr $argument_count - 1`
for i in $(seq 1 $argument_count); do
    arg="$1"
    shift
    # ...
    val=`echo "$arg" | sed -e 's;^--[^=]*=;;'`
    case "$arg" in
    --user=*|--username=*) USERNAME_0="$val" ;;
    --pass=*|--password=*) PASSWORD_0="$val" ;;
    --db-pass=*|--db-password=*) DB_PASSWORD_0="$val" ;;
    --db-host=*) DB_HOST_0="$val" ;;
    --db-port=*) DB_PORT_0="$val" ;;
    --dataxone-base=*) DATAXONE_BASE_0="$val" ;;
    --webapps=*) DATAXONE_WEB_APPS_0="$val" ;;
    --dataxone-webapps=*) DATAXONE_WEB_APPS_1="$val" ;;
    --pwdcrypt-dir=*) PWDCRYPT_DIR_0="$val" ;;
    --manager-pwdcrypt-dir=*) MANAGER_PWDCRYPT_DIR_0="$val" ;;
    --skip-precheck) DBS_SKIP_PRECHECK=1 ;;
    --debug) DEBUG=1 ;;
    esac
done


case $command in
db|os) ;;
-v|--version|version) echo "$SCRIPT_NAME Ver $VERSION, Updated on $UPDATE" && exit 0 ;;
-h|--help|*) Usage ;;
esac

[ -z "$DB_PASSWORD_0" ] && echo "[ERROR] Illegal Argument: --password is empty." && Usage

if [ "$DEBUG" != "0" ]; then
    echo "
[DEBUG] --pwdcrypt-dir=$PWDCRYPT_DIR_0
[DEBUG] --manager-pwdcrypt-dir=$MANAGER_PWDCRYPT_DIR_0
[DEBUG] --password=$PASSWORD_0
[DEBUG] --db-password=$DB_PASSWORD_0
"
fi


# 修改前测试连接是否成功
test_mysql_conn(){
    export LANG=zh_CN.utf8;
    export MYSQL_PWD="$DB_PASSWORD_0";
    test_mysql_conn_result=$($MYSQL_CLIENT -h${DB_HOST_0} -u${USERNAME_0:-dataxone} -P${DB_PORT_0:-3306} -e 'exit' 2>&1 | egrep 'Access denied|ERROR')
    if [ -n "$test_mysql_conn_result" ]; then
        echo "$test_mysql_conn_result"
        exit -1
    fi

}

update_config_file(){
    export LANG=en_US.utf8
    echo "`date` Updating $1"
    sh $CONFIG_MASTER_SCRIPT $1 $2
    #cat $2 | egrep -v 'module.c.mysql.password|ftp_collection_password'
    echo
}

update_yml_config_file(){
    export LANG=en_US.utf8
    echo "`date` Updating $1"
    sh $YML_MASTER_SCRIPT set --yml-file=$1 --input-props-file=$2
    #sh $YML_MASTER_SCRIPT get --yml-file=$1 --key=spring.datasource.password
    echo
}

#logfile=./dataxone-pwd-updater-$command-$(date +'%Y-%m-%d_%H%M%S')
#exec 1> >(tee -a /dev/tty > $logfile)
#exec 2>&1
echo "DataXone Password Updater Ver $VERSION, Updated on $UPDATE"
echo

case $command in
db) 
    en_pwd=$(cd $PWDCRYPT_DIR_0 && ./pwdcrypt.sh "$DB_PASSWORD_0" | awk 'NR==19')
    de_pwd=$(cd $PWDCRYPT_DIR_0 && ./pwddecrypt.sh "$DB_PASSWORD_0" | awk 'NR==19')
    [ "$DEBUG" != "0" ] && echo "[DEBUG] en_pwd=$en_pwd"
    [ "$DEBUG" != "0" ] && echo "[DEBUG] de_pwd=$de_pwd"
    [ "$de_pwd" == "$DB_PASSWORD_0" ] && echo "[INFO] Encryption and decryption check passed." || echo "[ERROR] Encryption/decryption check failed" && exit -1

    [ "$DBS_SKIP_PRECHECK" != "1" ] && test_mysql_conn

    en_mgr_db_pwd=$(cd $MANAGER_PWDCRYPT_DIR_0 && ./encrypt.sh "$DB_PASSWORD_0")
    [ "$DEBUG" != "0" ] && echo "[DEBUG] en_mgr_db_pwd=$en_mgr_db_pwd"

    # START
    cat > $TMP_FILE <<EOF
mysqlmonitor.jdbc.password=ENC($en_pwd)
EOF
    # DataXone_Query_Engine
    update_config_file $(get_config_file DataXone_Query_Engine) $TMP_FILE
    cat > $TMP_FILE <<EOF
spring.datasource.password=ENC($en_pwd)
EOF
    # permission
    update_config_file $(get_config_file permission) $TMP_FILE
    # autoMaticEngineBoot-1.0.0
    update_config_file $(get_config_file autoMaticEngineBoot-1.0.0) $TMP_FILE
    # Only ICBC Version
    # icbc_automatic_flow_dsg_project-1.0.0
    update_yml_config_file $(get_config_file icbc_automatic_flow_dsg_project-1.0.0) $TMP_FILE
    # sbt_xcmp
    cat > $TMP_FILE <<EOF
spring.datasource.password=ENC($en_pwd)
module.c.mysql.password=$en_mgr_db_pwd
EOF
    # sbt_xcmp
    update_config_file $(get_config_file sbt_xcmp) $TMP_FILE

    # Manager_Node_Engine
    cat > $TMP_FILE <<EOF
spring.datasource.password=$en_mgr_db_pwd
EOF
    update_config_file $(get_config_file Manager_Node_Engine) $TMP_FILE
    
    # END
    
;;
os) 
    en_pwd=$(cd $PWDCRYPT_DIR_0 && ./pwdcrypt.sh "$PASSWORD_0" | awk 'NR==19')
    de_pwd=$(cd $PWDCRYPT_DIR_0 && ./pwddecrypt.sh "$en_pwd" | awk 'NR==19')
    [ "$DEBUG" != "0" ] && echo "[DEBUG] en_pwd=$en_pwd"
    [ "$DEBUG" != "0" ] && echo "[DEBUG] de_pwd=$de_pwd"
    [ "$de_pwd" == "$PASSWORD_0" ] && echo "[INFO] Encryption and decryption check passed." || echo "[ERROR] Encryption/decryption check failed" && exit -1

    # Only ICBC Version
    cat > $TMP_FILE <<EOF
tomcat1.pwd=ENC($en_pwd)
tomcat2.pwd=ENC($en_pwd)
ftp_collection_password=$PASSWORD_0
EOF
    # autoMaticEngineBoot-1.0.0
    update_config_file $(get_config_file autoMaticEngineBoot-1.0.0) $TMP_FILE

    export LANG=zh_CN.utf8;
    export MYSQL_PWD="$DB_PASSWORD_0";

    $MYSQL_CLIENT -h${DB_HOST_0} -u${USERNAME_0:-dataxone} -P${DB_PORT_0:-3306}<<EOF
update dataxone_icbc_module.icbc_param_config_tb set parame_value='$PASSWORD_0' where param_key = 'password';
exit;
EOF
    
;;
esac

echo
echo "$SCRIPT_NAME completed. Next, Restart to take effect"
echo 

echo "
【说明】
1、脚本执行完成后，下一步应该重启对应的服务，包括 tomcat、dataXoneTomcat、DataXone_Query_Engine/query_db_engine和Manager_Node_Engine。
2、当平台有存量链路时，需要具体链路执行如下操作：
  1) 更多->修改->完成并校验； 
  2) 停止->启动。

"

rm $TMP_FILE